<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html><head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>db.select 查询 (web.py)</title> 
  <link rel="stylesheet" type="text/css" href="/static/webpy-new.css"/> 
  <link rel="SHORTCUT ICON" href="/static/favicon.ico"/> 
</head><body> 
 
        <div id="header"><table class="header"><tr>
                <td class="logo"><a href="/" title="Home"><img src="/static/webpy.gif" alt="" /></a></td> 
<td class="blurb">&ldquo;Think about the ideal way to write a web app.<br />Write the code to make it happen.&rdquo; [<a href="/philosophy">more...</a>]</td> </tr>
</table>
        </div> 
        <div id="location"> 
                <a href="/">home</a> &gt; db.select 查询
        </div> 
<div id="container"> 
<div id="sidebar">
<strong>get started:</strong>
<ul class="body">
  <li><a href="/install">install</a></li>
  <li><a href="/docs/0.3/tutorial">tutorial</a></li>
</ul>
<strong>learn more:</strong>
<ul class="body">
<li><a href="/docs/0.3/api">api reference</a></li>
<li><a href="/cookbook/">cookbook</a></li>
<li><a href="/src/">code examples</a></li>
</ul>

<strong>dive in:</strong>
<ul class="body"> 
<li><a href="https://github.com/webpy/webpy">source code</a></li>
<li><a href="http://groups.google.com/group/webpy/">mailing list</a></li>
<li><a href="https://github.com/webpy/webpy/issues">issue tracker</a></li>
</ul>
</div>

<div id="main"> 
<div id="content" class="body"> 
<h1>db.select 查询</h1>

<h2>问题:</h2>

<p>怎样执行数据库查询？</p>

<h2>解决方案:</h2>

<p>如果是0.3版本, 连接部分大致如下:</p>

<pre><code>db = web.database(dbn='postgres', db='mydata', user='dbuser', pw='')
</code></pre>

<p>当获取数据库连接后, 可以这样执行查询数据库:</p>

<pre><code># Select all entries from table 'mytable'
entries = db.select('mytable')
</code></pre>

<p>select方法有下面几个参数:</p>

<ul>
<li>vars</li>
<li>what</li>
<li>where</li>
<li>order</li>
<li>group</li>
<li>limit</li>
<li>offset</li>
<li>_test</li>
</ul>


<h3>vars</h3>

<p>vars变量用来填充查询条件.  如:</p>

<pre><code>myvar = dict(name="Bob")
results = db.select('mytable', myvar, where="name = $name")
</code></pre>

<h3>what</h3>

<p>what是标明需要查询的列名, 默认是*, 但是你可以标明需要查询哪些列.</p>

<pre><code>results = db.select('mytable', what="id,name")
</code></pre>

<h3>where</h3>

<p>where查询条件, 如:</p>

<pre><code>results = db.select('mytable', where="id&gt;100")
</code></pre>

<h3>order</h3>

<p>排序方式:</p>

<pre><code>results = db.select('mytable', order="post_date DESC")
</code></pre>

<h3>group</h3>

<p>按group组排列.</p>

<pre><code>results = db.select('mytable', group="color")    
</code></pre>

<h3>limit</h3>

<p>从多行中返回limit查询.</p>

<pre><code>results = db.select('mytable', limit=10) 
</code></pre>

<h3>offset</h3>

<p>偏移量, 从第几行开始.</p>

<pre><code>results = db.select('mytable', offset=10) 
</code></pre>

<h3>_test</h3>

<p>查看运行时执行的SQL语句:</p>

<pre><code>results = db.select('mytable', offset=10, _test=True) 
&lt;sql: 'SELECT * FROM mytable OFFSET 10'&gt;
</code></pre>

<div style="clear: both;"></div> 
 
</div> 
 
</div> 
</div>
        <div id="footer"> 
    <p>started by <a style="color: gray;" href="http://www.aaronsw.com/">Aaron Swartz</a> (<a style="color: black;" href="mailto:webpy@aaronsw.com">webpy@aaronsw.com</a>)</p>

<p><a href="https://github.com/webpy/webpy.github.com/blob/master/cookbook/select.zh-cn.md">fork this page on GitHub</a></p>
</div> 
 
</body> 
</html> 
